<?php

namespace app\comapi\controller;

use app\common\controller\Frontend;
use app\common\enums\ArticleTTlStatusEnum;
use app\common\model\banxi\course\CourseArticle;
use app\common\model\banxi\learn\LearnReadingArticleExtend;
use app\common\service\TencentVocalService;

/**
 * 配置
 */
class CallBack extends Frontend
{
    protected array $noNeedLogin = ['*'];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    public function ttlCallback()
    {
        $param = $this->request->param('data');
        $voiceCallback = html_entity_decode($param, ENT_QUOTES | ENT_HTML5, 'UTF-8');
        $voiceCallback = json_decode($voiceCallback, true);
        $taskId = $voiceCallback['TaskId'];
        $courseArticleModel = new CourseArticle();
        $count = $courseArticleModel->where('ttlTaskId', $taskId)->count();
        if ($count > 0) {
            $tencentVocalService = new TencentVocalService();
            $voiceUrl = $tencentVocalService->longTextToVoiceCallback($voiceCallback);
            $status = ArticleTTlStatusEnum::E0;
            switch ($voiceCallback['Status']) {
                case 1:
                    $status = ArticleTTlStatusEnum::E1;
                    break;
                case 2:
                    $status = ArticleTTlStatusEnum::E2;
                    break;
                case 3:
                    $status = ArticleTTlStatusEnum::E3;
                    break;
            }
            $courseArticleModel->update([
                'contentVoice' => $voiceUrl,
                'ttlStatus' => $status,
            ], ['ttlTaskId' => $taskId]);
        }
        return true;
    }

    /*
     * Ai文章生成回调
     * */
    public function aiArticlettlCallback()
    {
        $param = $this->request->param('data');
        $voiceCallback = html_entity_decode($param, ENT_QUOTES | ENT_HTML5, 'UTF-8');
        $voiceCallback = json_decode($voiceCallback, true);
        $taskId = $voiceCallback['TaskId'];
        $LearnReadingArticleExtendModel = new LearnReadingArticleExtend();
        $extend = $LearnReadingArticleExtendModel->where('ttlTaskId', $taskId)->find();
        if ($extend) {
            $tencentVocalService = new TencentVocalService();
            $voiceUrl = $tencentVocalService->longTextToVoiceCallback($voiceCallback);
            $LearnReadingArticleExtendModel->update([
                'articleContentVoice' => $voiceUrl
            ], ['ttlTaskId' => $taskId]);
        }
        return true;
    }


}